﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIT.Controls.Containers;
using System.Windows;
using System.Reflection;
using UIT.Controls;
using UIT.Controls.Input;
using UIT.Controls.Data;
using UIT.Controls.Template;

#if !SILVERLIGHT
namespace UIT.WPF.Renderers
#else
namespace UIT.Silverlight.Renderers
#endif
{
#if !SILVERLIGHT
    public class WpfRendererManager
#else
    public class SlRendererManager
#endif
    {
        internal static void EnsureInitialized()
        {
            if (initialized)
                return;
            initialized = true;
            RendererManager.TypeNeeded += new Func<string, Type>(RendererManager_TypeNeeded);
            RendererManager.ReferencesNeeded += new Action<List<System.Reflection.Assembly>>(RendererManager_ReferencesNeeded);
            RendererManager.Mode = RenderingMode.WPF;
        }

        static Assembly[] references;

        static void RendererManager_ReferencesNeeded(List<System.Reflection.Assembly> references)
        {
            references.Add(Application.Current.GetType().Assembly);
            references.AddRange(AppDomain.CurrentDomain.GetAssemblies());
#if !SILVERLIGHT
            WpfRendererManager.references = references.ToArray();
#else
            SlRendererManager.references = references.ToArray();
#endif
        }

        static Type RendererManager_TypeNeeded(string arg)
        {
            foreach (Assembly assembly in references)
            {
                Type t = assembly.GetType(arg);
                if (t != null)
                    return t;
            }
            return null;
        }

#if !SILVERLIGHT
        public WpfRendererManager(Application application)
#else
        public SlRendererManager(Application application)
#endif
        {
            this.application = application;
        }

        RendererManager manager;
        private static bool initialized;
        internal RendererManager Manager
        {
            get
            {
                if (manager == null)
                {
                    manager = new RendererManager(Initialize);
                    manager.Control.RegisterRenderer<ButtonRenderer, Button>();
                    manager.Control.RegisterRenderer<PageRenderer, Page>();
                    manager.Control.RegisterRenderer<TextBoxRenderer, TextBox>();
                    manager.Control.RegisterRenderer<TextBlockRenderer, TextBlock>();
                    //manager.RegisterControlRenderer<FormRenderer, Form>();
                    //manager.RegisterControlRenderer<RepeaterRenderer, Repeater>();
                    //manager.RegisterControlRenderer<LineBreakRenderer, LineBreak>();
                    //manager.RegisterControlRenderer<HtmlControlRenderer, HtmlControl>();
                    manager.Control.RegisterRenderer<DataGridRenderer, DataGrid>();
                    manager.Control.RegisterRenderer<GridRenderer, Grid>();
                    //manager.RegisterTemplateRenderer<ClientTemplateRenderer, ClientTemplate>();
                    manager.Control.RegisterRenderer<StackPanelRenderer, StackPanel>();
                    manager.Control.RegisterRenderer<WrapPanelRenderer, WrapPanel>();
                    manager.Control.RegisterRenderer<ItemsControlRenderer, ItemsControl>();
                    manager.Control.RegisterRenderer<ItemsControlRenderer, SelectableItemsControl>();
                    manager.Control.RegisterRenderer<DataTemplateInstanceRenderer, DataTemplateInstance>();
                    manager.Control.RegisterRenderer<BorderRenderer, Border>();
                    manager.Control.RegisterRenderer<WpfControlHostRenderer, WpfControlHost>();
                    manager.Control.RegisterRenderer<ScrollViewerRenderer, ScrollViewer>();
                }
                return manager;
            }
        }

        private void Initialize(IRenderer renderer)
        {
            IRenderer webRenderer = renderer as IRenderer;
            if (webRenderer != null)
                webRenderer.Initialize(Manager, null);
        }

        public static UIElement Render(Uri uri)
        {
#if !SILVERLIGHT
            System.Windows.Resources.StreamResourceInfo streamInfo = Application.GetResourceStream(uri);
            return Render(RendererManager.CreateInstance<Page>(streamInfo.Stream, false));
#else
            return Render(RendererManager.CreateInstance<Page>(uri));
#endif
        }

        public static UIElement Render(Page p)
        {
            return RendererManager.Render(p, typeof(Page));
        }

        public Application application { get; set; }

        public static RendererManager RendererManager
        {
            get { return UIT.Context.Current.RendererManager; }
        }
    }
}
